import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import Home from '../components/Home.vue'
import Welcom from '../components/Welcom.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Parmas from '../components/goods/Parmas.vue'
import List from '../components/goods/List.vue'
import Add from '../components/goods/Add.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'

Vue.use(VueRouter)
const router = new VueRouter({
        routes: [
            { path: '/', redirect: '/login' },
            {
                path: '/login',
                component: login
            },
            {
                path: '/home',
                component: Home,
                redirect: '/welcom',
                children: [{
                        path: '/welcom',
                        component: Welcom
                    }, {
                        path: '/users',
                        component: Users
                    },
                    {
                        path: '/rights',
                        component: Rights
                    },
                    {
                        path: '/roles',
                        component: Roles
                    },
                    {
                        path: '/categories',
                        component: Cate
                    },
                    {
                        path: '/params',
                        component: Parmas
                    },
                    {
                        path: '/goods',
                        component: List
                    },
                    {
                        path: '/goods/add',
                        component: Add
                    },
                    {
                        path: '/orders',
                        component: Order
                    },
                    {
                        path: '/reports',
                        component: Report
                    }
                ]
            }
        ]
    })
    // 挂载路由守卫,next()表示放行，next('/login')表示强制跳转
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next();
    const token_str = window.sessionStorage.getItem('token');
    if (!token_str) return next('/login');
    next();
})
export default router